/*

Author   : chaos

FileName : fptr_in.cpp

Date     : 16/08/17 14:22:32

*/
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

#define FILEPATH "Reader.bin"
int main(int argc ,char *argv[])
{
    fstream file;
    char str[4][8] = {"Mary","John","Judy","Joe"};

    int id [4] = {100,200,300,400};

    file.open(FILEPATH,ios::out | ios::in | ios::binary | ios::trunc);

    if(!file)
    {
        cerr << "Can't open file!" << endl;
    }
    for (int i = 0; i < 4 ; i ++)
    {
        file.write((char *) &id[i], sizeof(int));
        file.write(str[i],8*sizeof(char));
    }


    int id1;
    int record_size = sizeof(int) + 8 * sizeof(char);
    char str1[8];

    file.seekg(0,ios::end);
    cout << "record size : " << record_size
        << "file size :" << file.tellg() << endl;

    cout << setw(4) << setiosflags(ios::right) << "ID"
         <<  setw(8) << setiosflags(ios::right) << "Name" << endl;
    cout << "---------------------------------" << endl;

    file.seekg(record_size* 2,ios::beg);

    file.read((char*) &id1, sizeof(int));
    file.read(str1, 8*sizeof(char));

    cout << setw(4) << setiosflags(ios::right) << id1
        << setw(8) << setiosflags(ios::right) << str1 << endl;

    return 0;
}
